﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
using System.Collections;

namespace AllegroWebAPI
{

    public class BaseAllegroExt
    {
        protected AllegroClient aapi;
        public BaseAllegroExt(AllegroClient _api)
        {
            aapi = _api;
        }
    }


    /// <summary>
    /// Helper class
    /// </summary>
    [Serializable()]
    public class IdName
    {
        public int Id { get; set; }
        public string Name { get; set; }
        
        public IdName(int id, string nam)
        {
            Id = id;
            Name = nam;
        }

        public IdName()
        {
            Id = 0;
            Name = "";
        }
    }

    /*
    [Serializable()] 
    public class GenericDictionary<T>
    {
        public Dictionary<string, T> Items;

        public GenericDictionary()
        {
            Items = new Dictionary<string, T>();
        }

        public void Add(string key, T item)
        {
            Items.Add(key, item);
        }

        public void LoadFromFile(string fileName)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(Dictionary<string, T>));
            TextReader textReader = new StreamReader(fileName);
            Items = (Dictionary<string, T>)deserializer.Deserialize(textReader);
            textReader.Close();
        }


        public void SaveToFile(string fileName)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Dictionary<string, T>));
            TextWriter textWriter = new StreamWriter(fileName);
            serializer.Serialize(textWriter, Items);
            textWriter.Close();
        }
    }
    */
    [Serializable()]    //Set this attribute to all the classes that want to serialize
    public class GenericList<T> : IEnumerable
    {
        public List<T> Items;

        public GenericList()
        {
            Items = new List<T>();
        }

        public void Add(T item)
        {
            Items.Add(item);
        }

        public void LoadFromFile(string fileName)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(List<T>));
            TextReader textReader = new StreamReader(fileName);
            Items = (List<T>)deserializer.Deserialize(textReader);
            textReader.Close();
        }


        public void SaveToFile(string fileName)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<T>));
            TextWriter textWriter = new StreamWriter(fileName);
            serializer.Serialize(textWriter, Items);
            textWriter.Close();
        }

        public IEnumerator<T> GetEnumerator()
        {
            return this.Items.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

    }
    
}
